package com.ruoyi.web.controller.system;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.Share;
import com.ruoyi.system.domain.bo.FileBo;
import com.ruoyi.system.domain.vo.FileVo;
import com.ruoyi.system.service.IFileService;
import com.ruoyi.system.service.IShareService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 分享
 *
 * @author ruoyi
 * @date 2023-08-02
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/file")
public class FileController extends BaseController {

    private final IFileService iFileService;
    private final IShareService iShareService;

    /**
     * 查询分享列表
     */
    @SaCheckLogin
    @GetMapping("/list")
    public TableDataInfo<FileVo> list(FileBo bo, PageQuery pageQuery) {
        return iFileService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出分享列表
     */
    @SaCheckLogin
    @Log(title = "分享", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(FileBo bo, HttpServletResponse response) {
        List<FileVo> list = iFileService.queryList(bo);
        ExcelUtil.exportExcel(list, "分享", FileVo.class, response);
    }

    /**
     * 获取分享详细信息
     *
     * @param id 主键
     */
    @SaCheckLogin
    @GetMapping("/{id}")
    public R<FileVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iFileService.queryById(id));
    }

    /**
     * 新增分享
     */
    @SaCheckLogin
    @Log(title = "分享", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody FileBo bo) {
        return toAjax(iFileService.insertByBo(bo));
    }

    /**
     * 修改分享
     */
    @SaCheckLogin
    @Log(title = "分享", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody FileBo bo) {
        return toAjax(iFileService.updateByBo(bo));
    }

    /**
     * 删除分享
     *
     * @param ids 主键串
     */
    @SaCheckLogin
    @Log(title = "分享", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iFileService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    /**
     * 根据code获取文件列表
     * @param code
     * @return
     */
    @SaIgnore
    @GetMapping("getListByCode")
    public R<List<FileVo>> getListByCode(Long code,@RequestParam(value = "userId",defaultValue = "0",required = false) Long userId){
        List<FileVo> fileVoList = iFileService.getListByCode(code, userId);
        Share share = iShareService.queryByCode(code);
        return fileVoList.isEmpty()?R.fail("文件不存在或已过期"):R.ok(share.getRemark(),fileVoList);
    }

    /**
     * 下载文件
     * @param id
     * @return
     */
    @SaIgnore
    @GetMapping("download/{id}")
    public R<Void> download(@PathVariable(value = "id") Long id){
        FileVo fileVo = iFileService.queryById(id);
        if(fileVo==null){
            return R.fail("文件数据不存在!");
        }
        fileVo.setDownloadCount(fileVo.getDownloadCount()+1);
        return toAjax(iFileService.updateByBo(BeanUtil.toBean(fileVo, FileBo.class)));
    }
}
